package cn.bj.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.bj.beans.Advertisement_picture;
import cn.bj.service.AdvertisementService;

/*
 * 
 * 广告图控制层
 * */
@Controller
@RequestMapping("/admin")
public class AdvertisementController {

	@Autowired
	AdvertisementService advertisementSservice;

	/*
	 * 查询所有
	 */
	/*
	 * @RequestMapping("") public String selectAllAdver(){
	 * List<Advertisement_picture> advers =
	 * advertisementSservice.selectAllAdvers(); //放入请求域中 return ""; }
	 */

	// 新增广告图
	@PostMapping("/insertAdver.do")
	@ResponseBody
	public String insert(@Param("adverUrl") String adverUrl, HttpSession session, @Param("img") MultipartFile img)
			throws IllegalStateException, IOException {
		// 返回信息
		String msg;
		if (img != null) {
			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/AdverImg");
			// 文件名以原来文件名命名
			String ImgName = img.getOriginalFilename();
			// 限制上传的文件名的后缀名为jpg、png
			if (ImgName.endsWith("jpg") || ImgName.endsWith("png")) {
				// 装载文件
				File file = new File(path, ImgName);
				// 上传文件
				img.transferTo(file);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/AdverImg/" + ImgName;

				// 新生图片
				Advertisement_picture adver = new Advertisement_picture();
				adver.setAdvertisement(path);
				adver.setAdvertisementUrl(adverUrl);
				int num2 = advertisementSservice.insertAdver(adver);

				if (num2 != 0) {
					msg = "添加成功";
					System.out.println(msg);
					return msg;
				} else {
					msg = "添加失败";
					return msg;
				}

			}
			msg = "图片格式不正确，请确认图片格式jpg/png";
			return msg;
		}
		msg = "文件不能为空";
		return msg;
	}

	// 删除
	@RequestMapping("/deleteAdver.do")
	public String deleteP(@Param("id") Integer id) {
		int num3 = advertisementSservice.deleteAdver(id);
		System.out.println(num3);
		return "redirect:/admin/AllAdmins.do";
	}

	// 跳转修改图片,拿到单个对象
	@RequestMapping("/updateAdver.do")
	public String GotoUpdateAdver(@Param("id") Integer id, Model model) {
		Advertisement_picture adverp = advertisementSservice.selectById(id);
		model.addAttribute("adverp", adverp);
		return "admin/updateAdver";
	}

	// 修改图片
	@RequestMapping("/AdverUpdate.do")
	public String updateRotate(Advertisement_picture adverp, HttpSession session, @Param("img") MultipartFile img)
			throws IllegalStateException, IOException {
		// 返回信息
		System.out.println("---------" + img.getSize());
		if (img.getSize() > 0) {
			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/AdverImg");
			// 文件名以原来文件名命名
			String ImgName = img.getOriginalFilename();
			// 限制上传的文件名的后缀名为jpg、png
			if (ImgName.endsWith("jpg") || ImgName.endsWith("png")) {
				// 装载文件
				File file = new File(path, ImgName);
				// 上传文件
				img.transferTo(file);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/AdverImg/" + ImgName;

				// 修改图片
				adverp.setAdvertisement(path);

				int num2 = advertisementSservice.updateAdverP(adverp);

				if (num2 != 0) {
					return "redirect:/admin/AllAdmins.do";
				}
				return "admin/error2";

			}
		} else {
			// 为空操作
			advertisementSservice.updateAdverP(adverp);
			return "redirect:/admin/AllAdmins.do";
		}
		return "admin/error2";

	}

}
